home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / LP.ARJ / LP.CPP < prev    next >
C/C++ Source or Header  |  1991-05-21  |  11KB  |  356 lines

  1. /******************************************************
  2. *                  ** COPYRIGHT **                    *
  3. *   to this program's source code                     *
  4. *   belongs to Dudley Farthing Jr.                    *
  5. *   CIS UID   : [71240,2557]                          *
  6. *                                                     *
  7. *            -  YOU MAY COPY FREELY,  -               *
  8. *      -  Please Note Any Changes You Make  -         *
  9. *          -  In Coppies You Give Away  -             *
  10. *   - AND NEVER CHARGE FOR THE PROGRAM ITSELF -       *
  11. *          -  (unless i get some too)  -              *
  12. *         @-,'---       >;->       ---,'-@            *
  13. *                                                     *
  14. *   Creation Date : 19910512213453 LCT                *
  15. *   Last Revision : 19910516160316 LCT                *
  16. *   Purpose : Printer Code transmission from an Ascii *
  17. *             file that is edited/maintained by the   *
  18. *             user.                                   *
  19. ******************************************************/
  20.  
  21. #include<bios.h>
  22. #include<stdio.h>
  23. #include<stdlib.h>
  24. #include<string.h>
  25.  
  26. const unsigned short DEF_PRT = 1,     // Default Printer
  27.                    MAX_FLAGS = 100,   // Max Flags passed from Com. line.
  28.                 MAX_FLAG_LEN = 30,    // Max Flag length in Char's
  29.            MAX_FLAG_DESC_LEN = 50,    // Max Flag Description len. (char's)
  30.              MAX_CODESTR_LEN = 160,   // Max Text Printer Code len. (char's)
  31.                 MAX_CODE_LEN = 80,    // Max Printer Code len. (codes)
  32.                    MAX_CODES = 200;   // Max Codes read in from file
  33.  
  34. const char *DEF_FILENAME="lp_codes.out\x0                              "\
  35.                         "                                           \x0",
  36.                *P_ERROR="The selected printer is not ready...\x0       ",
  37.                  *USAGE="Usage lp [-fUFN] [-pn] [-l] [-?] [-d] flag [fl"\
  38.                         "ag] [flag] ..\n\x0                            ",
  39.                 *UN_REC="Unrecognized Flag Error, -l to view the file. "\
  40.                         "\n\n\x0                                       ",
  41.            *P_SELECTION="Printer error : Selection\n\x0                ",
  42.            *F_SELECTION="File error : Selection\n\x0                   ",
  43.            *H_SELECTION="Usage lp [-fUFN] [-pn] [-d] [-l] [-?] flag [fl"\
  44.                         "ag] [flag] ..\nthe directives may be in any or"\
  45.                         "der.\nthe -d directive will cause no ouput to "\
  46.                         "the screen to occure\nexcept the codes themsel"\
  47.                         "ves.\nthe -f directive preceeds an un-ambiguis"\
  48.                         "\nfile name. If the file isn't in the current "\
  49.                         "directory, the PATH is searched.\nthe -l direc"\
  50.                         "tive causes the file contents to be listed.\nt"\
  51.                         "he -p option followed by an integer that corre"\
  52.                         "lates to LPT_:\nflags are text strings. they m"\
  53.                         "ay not contain space characters,\nand are case"\
  54.                         " sensative.\n\x0                              ",
  55.               *MF_ERROR="Too many Flags Passed\n\x0                    ",
  56.                *M_ERROR="Memory Allocation Error\x7\n\x0               ",
  57.             *UNREC_FLAG="Unrecognized directive...\n\x0                ",
  58.                   *PATH="PATH\x0                                       ",
  59.                   *MODE="r\x0                                          ",
  60.                FLAG_CHR='-',
  61.             DIRECT_FLAG='d',
  62.                PRT_FLAG='p',
  63.               FILE_FLAG='f',
  64.               LIST_FLAG='l',
  65.               HELP_FLAG='?';
  66.  
  67. typedef class _p {
  68.          private:
  69.              char *desc,
  70.                   *code;
  71.           public:
  72.              char *flag;
  73.              void code_convert();
  74.             _p();
  75.            ~_p();
  76.              void printf(short,short);
  77.               int fread(FILE*fp);
  78.                  } pr_code;
  79.  
  80.                    void die(int,
  81.                             const char*);
  82.  
  83.                   int fread(pr_code*,
  84.                             FILE*);
  85.  
  86.     unsigned int parse_args(char**,
  87.                             FILE**,
  88.                             short*,
  89.                             char**,
  90.                             short*,
  91.                             char**,
  92.                             short*);
  93.  
  94. unsigned int scrounge_codes(pr_code*,
  95.                             FILE*);
  96.  
  97.             void dump_codes(char**,
  98.                             pr_code*,
  99.                             short,
  100.                             unsigned int,
  101.                             unsigned int,
  102.                             short);
  103.  
  104.              FILE *file_fnd(char**,
  105.                             char*,
  106.                             const char *);
  107.  
  108.                    int comp(const void*,
  109.                             const void*);
  110.  
  111.                   int _comp(const void*,
  112.                             const void*);
  113.  
  114. void main(int argc,char **argv,char **env)
  115. {
  116.   short printer=DEF_PRT,
  117.         lst=0,
  118.         te,
  119.         direct=0;
  120.   unsigned int n_flags,
  121.                n_codes;
  122.   char *f_name=(char*)malloc(strlen(DEF_FILENAME)+1),
  123.        **flags=(char**)malloc(MAX_FLAGS*sizeof(char*));
  124.   FILE *infile=NULL;
  125.   for (unsigned int q=0;q<MAX_FLAGS;q++)
  126.     if (!(*(flags+q)=(char*)malloc(MAX_FLAG_LEN))) die(1,M_ERROR);
  127.   pr_code codes[MAX_CODES];
  128.   if (!codes) die(1,M_ERROR);
  129.   if (!--argc)
  130.     die(-1,USAGE);
  131.   n_flags=parse_args(++argv,&infile,&printer,flags,&lst,env,&direct);
  132.   if (!infile)
  133.   {
  134.     strcpy(f_name,DEF_FILENAME);
  135.     infile=file_fnd(env,f_name,MODE);
  136.   }
  137.   if (!infile)
  138.     die(1,F_SELECTION);
  139.   n_codes=scrounge_codes(codes,infile);
  140.   fclose(infile);
  141.   if (lst)
  142.     for (unsigned int s=0;s<n_codes;s++) codes[s].printf(0,0);
  143.   qsort(codes,n_codes,sizeof(pr_code),comp);
  144.   if (n_flags)
  145.   {
  146.     if (!direct) fprintf(stderr,"Codes Passed : \n");
  147.     dump_codes(flags,codes,printer,n_flags,n_codes,direct);
  148.   }
  149.   die(0,"");
  150. }
  151.  
  152. void dump_codes(char **flags,pr_code *codes,short printer,
  153.                 unsigned int t_flags,unsigned int t_codes,short df)
  154. {
  155.   pr_code *tmp;
  156.   unsigned int q=0u;
  157.   do
  158.   {
  159.     tmp=(pr_code*)bsearch(flags[q],codes,t_codes,sizeof(pr_code),_comp);
  160.     if (!tmp)
  161.       die (-1,UN_REC);
  162.     else
  163.       tmp->printf(printer,df);
  164.     q++;
  165.   }
  166.   while (flags[q][0] && q<t_flags);
  167. }
  168.  
  169. void pr_code::printf(short l,short df)
  170. {
  171.   int stat_result=0;
  172.   unsigned short q=0u;
  173.   if (l)
  174.     while(*(code+q))
  175.       if (df)
  176.         fwrite(code+q++,1,1,stdout);
  177.       else
  178.       {
  179.         stat_result=biosprint(2,stat_result,(int)l-1);
  180.         if (!((stat_result & 0x40) || (stat_result & 0x80)))
  181.           die (-1,P_ERROR);
  182.         biosprint(0,*(code+q++),(int)l-1);
  183.       }
  184.   if (!df)
  185.     fprintf(stderr,"%s -> %s\n",flag,desc);
  186. }
  187.  
  188. int pr_code::fread(FILE *fp)
  189. {
  190.   int ret=1;
  191.   ret&=(fgets(flag,MAX_FLAG_LEN,fp)!=NULL);
  192.   *(flag+strlen(flag)-1)=0;   //  stop on \n at end of string
  193.   ret&=(fgets(desc,MAX_FLAG_DESC_LEN,fp)!=NULL);
  194.   *(desc+strlen(desc)-1)=0;   //  stop on \n at end of string
  195.   ret&=(fgets(code,MAX_CODESTR_LEN,fp)!=NULL);
  196.   *(code+strlen(code)-1)=0;   //  stop on \n at end of string
  197.   code_convert();             //  convert text num's Char's
  198.   return(ret);
  199. }
  200.  
  201. void pr_code::code_convert()
  202. {
  203.   unsigned short i=0u;
  204.   char *q=(char*)malloc(strlen(this->code)+1);
  205.   strcpy(q,this->code);
  206.   while (*q && *q==' ') q++;
  207.   while (*q)
  208.   {
  209.     *((this->code)+i++)=(char)atol(q);
  210.     while(*q && *q!=' ') q++;
  211.     while(*q && *q==' ') q++;
  212.   }
  213.   free(q);
  214.   *((this->code)+i)=0;
  215. }
  216.  
  217. unsigned int scrounge_codes(pr_code *p,FILE *fp)
  218. {
  219.   unsigned int recs=0u;
  220.   while ((recs < MAX_CODES) && (p+recs)->fread(fp))
  221.     recs++;
  222.   return(recs);
  223. }
  224.  
  225. int comp(const void*x,const void*y)
  226. {
  227.   pr_code *a=(pr_code*)x,
  228.           *b=(pr_code*)y;
  229.   return(strcmp(a->flag,b->flag));
  230. }
  231.  
  232. int _comp(const void*x,const void*y)
  233. {
  234.   pr_code *a;
  235.   char    *b;
  236.   a=(pr_code*)y;
  237.   b=(char*)x;
  238.   return(strcmp(b,a->flag));
  239. }
  240.  
  241. _p::_p()
  242. {
  243.   flag=(char*)malloc(MAX_FLAG_LEN);
  244.   desc=(char*)malloc(MAX_FLAG_DESC_LEN);
  245.   code=(char*)malloc(MAX_CODE_LEN);
  246. }
  247.  
  248. _p::~_p()
  249. {
  250.   free(flag);
  251.   free(desc);
  252.   free(code);
  253. }
  254.  
  255. void die(int y,const char *q)
  256. {
  257.   fcloseall();
  258.   if (*q)
  259.     fprintf(stderr,"%s",q);
  260.   exit(y);
  261. }
  262.  
  263. FILE *file_fnd(char **env,char *fname,const char *mode)
  264. {
  265.   FILE *infile=fopen(fname,mode);
  266.   unsigned short y=0u;
  267.   char **paths=(char**)malloc(80*sizeof(char*)),*w_file=fname,*tmp;
  268.   if (!paths) die(1,M_ERROR);
  269.   *paths=(char*)malloc(80*sizeof(char*));
  270.   if (infile) return(infile);
  271.   tmp=strchr(w_file,':');
  272.   if (tmp)
  273.     w_file=tmp+1;
  274.   while(tmp=strchr(w_file,'\\'))
  275.     w_file=tmp+1;
  276.   while(*(env) && strncmp(*(env),PATH,strlen(PATH)))
  277.     env++;
  278.   infile=fopen(w_file,mode);
  279.   if (*(env) && !infile)
  280.   {
  281.     char *q=*(env),*i;
  282.     q+=strlen(PATH);
  283.     while (*q==' ')
  284.       q++;
  285.     q++;
  286.     while (*q==' ')
  287.       q++;
  288.     while (*q)
  289.     {
  290.       i=strchr(q,';');
  291.       paths[y+1]=(char*)malloc(80*sizeof(char));
  292.       *paths[y+1]='\x0';
  293.       if (!paths[y]) die(1,M_ERROR);
  294.       strncpy(paths[y],q,i-q);
  295.       paths[y][(i-q)]=0;
  296.       strcat(paths[y],"\\");
  297.       strcat(paths[y],w_file);
  298.       q=i+1;
  299.       y++;
  300.     }
  301.     y=0;
  302.     while (*paths[y] && !infile)
  303.       infile=fopen(paths[y++],mode);
  304.    }
  305.    while (y+1)
  306.      free(paths[y--]);
  307.    free(paths);
  308.    return(infile);
  309. }
  310.  
  311. unsigned int parse_args(char **argv,FILE **fp,short *prt,char **flags,
  312.                         short *lst,char **env,short *dire)
  313. {
  314.   char *q;
  315.   unsigned int f_count=0u;
  316.   while (*(argv))
  317.   {
  318.     q=(*argv);
  319.     if (*q==FLAG_CHR)
  320.     {
  321.       q++;
  322.       if (*q==DIRECT_FLAG)
  323.         *dire=1;
  324.       else
  325.       if (*q==PRT_FLAG)
  326.       {
  327.         if(!(*prt=(short)atoi(q+1)))
  328.           die(-1,P_SELECTION);
  329.       }
  330.       else
  331.       if (*q==FILE_FLAG && *(q+1))
  332.       {
  333.         if(!(*fp=file_fnd(env,(q+1),MODE)))
  334.           die(1,F_SELECTION);
  335.       }
  336.       else
  337.       if (*q==LIST_FLAG)
  338.         *lst=1;
  339.       else
  340.       if (*q==HELP_FLAG)
  341.         die(0,H_SELECTION);
  342.       else
  343.         die(-1,UNREC_FLAG);
  344.     }
  345.     else
  346.     {
  347.       if (f_count>MAX_FLAGS) die(-1,MF_ERROR);
  348.       strcpy(flags[f_count],q);
  349.       f_count++;
  350.       flags[f_count][0]='\x0';
  351.     }
  352.     argv++;
  353.   }
  354.   return(f_count);
  355. }
  356.